//
// comm_standard.cpp
// xmlint
// An XML-based interface for the Protea Project / comm_standard.cpp defines communications standards
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

// Standard includes
#include <stdarg.h>

// Protea includes
#include "../SDK/ToolsInterface.h"
#include "comm_standard.h"

// Transaction Communication Standard
//////////////////////////////////////////////////////////////////////////////
const char lt[] = "<";
const char sl[] = "/";
const char gt[] = ">";

const char xmltag[] =			"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
const char includestag[] =		"Includes";
const char	includetag[] =			"Include";
const char proteatag[] =		"Protea";
const char	versiontag[] =			"Version";
const char errortag[] =				"Error";
const char	transactionlisttag[] =	"TransactionList";
const char		applnumbertag[] =		"ApplNumber";
const char		typetag[] =				"Type";
const char		usernametag[] =			"UserName";
const char		passwordtag[] =			"Password";
const char		languagetag[] =			"Language";
const char		sessiontag[] =			"Session";
const char		transactiontag[] =		"Transaction";
const char			numbertag[] =			"Number";
const char			idtag[] =				"ID";
const char			nametag[] =				"Name";
const char			successtag[] =			"Success";
const char			descriptiontag[] =		"Description";
const char			testidtag[] =			"TestID";
//const char			usernametag[] =			"UserName";
//const char			passwordtag[] =			"Password";
const char			ignoretag[] =			"Ignore";
const char			supertag[] =			"Super";
const char			actiontag[] =			"Action";
const char			phasetag[] =			"Phase";
//const char			languagetag[] =			"Language";
const char			leveltag[] =			"Level";
const char			bufferlisttag[] =		"BufferList";
const char				buffertag[] =			"Buffer";
//const char					numbertag[] =			"Number";
//const char					nametag[] =				"Name";
//const char					typetag[] =				"Type";
const char					requiredtag[] =			"Required";
const char					ordertag[] =			"Order";
const char					fieldtag[] =			"Field";
//const char						numbertag[] =			"Number";
//const char						nametag[] =				"Name";
const char			messagelisttag[] =		"MessageList";
const char				messagetag[] =			"Message";
//const char					numbertag[] =			"Number";
const char					severitytag[] =			"Severity";
//const char					requiredtag[] =			"Required";
//const char					fieldtag[] =			"Field";
//const char					ordertag[] =			"Order";

const char	querytransactiontag[] =	"Query";
//const char		applnumbertag[] =		"ApplNumber";
//const char		usernametag[] =			"UserName";
//const char		passwordtag[] =			"Password";
const char			counttag[] = "Count";
const char			starttag[] = "Start";
const char			distincttag[] = "Distinct";

//const char		buffertag[] =			"Buffer";
//const char			numbertag[] =			"Number";
//const char			nametag[] =				"Name";
const char				fieldlisttag[] =	"FieldList";
//const char					fieldtag[] =		"Field";
//const char						numbertag[] =		"Number";
//const char						nametag[] =			"Name";

const char				orderlisttag[] =		"OrderList";
//const char				ordertag[] =			"Order";
//const char						numbertag[] =		"Number";
//const char						nametag[] =			"Name";
const char					comparetag[] =				"Comparison";
const char					valuetag[] =				"Value";

const char				wherelisttag[] =		"WhereList";
const char					wheretag[] =			"Where";
const char						pretag[] =				"Pre";
//const char					numbertag[] =			"Number";
//const char					nametag[] =				"Name";
//const char					comparetag[] =			"Comparison";
const char						value1tag[] =			"Value1";
const char						value2tag[] =			"Value2";
const char						posttag[] =				"Post";
const char						logicaltag[] =			"Logical";

void XMLParseException::OutputError( XCSocketTransport* cursocket ) {
	// Any XML errors will makes us go here...
	xprintf( true, cursocket, "%s<%s %s=\"1.0\">\n\t<%s>%s</%s>\n</%s>\n", xmltag, proteatag, versiontag, errortag, svalue->value, errortag, proteatag );
};

const char* XMLParseException::GetDescription() {
	return svalue->value;
};




void xprintstr( bool last, XCSocketTransport *newsocket, const char* szBuffer, int nBuf ) {
	XMLTraceOut( szBuffer );

	// Do we need to get a string length?
	if (!nBuf)
		nBuf = strlen( szBuffer );

	// redirect output to cout or to socket
	if ( newsocket )
	{
		if (last)
			newsocket->Send( szBuffer, nBuf+1 );
		else
			newsocket->Send( szBuffer, nBuf );
	}
	else
	{
		printf( "***********\n DANGER NO SOCKET !!!! \n**************\n" );
		cout << szBuffer;
	};
};



void xprintf( bool last, XCSocketTransport *newsocket, const char *lpszFormat, ... )
{
	int nBuf;
	char* szBuffer = new char[ MAX_LEN ];
	va_list args;
	va_start(args, lpszFormat);

	// Fill in string parameters
	nBuf = vsnprintf( szBuffer, MAX_LEN, lpszFormat, args );

	// Write the string
	xprintstr( last, newsocket, szBuffer, nBuf );

	// Cleanup
	delete [] szBuffer;
	va_end(args);
};
